<template>
  <div class="app-container">
    <!-- 查询和其他操作 -->
    <div class="filter-container">

      <el-card class="box-card">
        <el-divider content-position="left">{{$t('总部原料库存列表')}}</el-divider>
        <el-card shadow="never" class="box-card">
          <div>
            <el-row :gutter="20">
              <el-col :span="6">
                <div>
                  <el-statistic
                      group-separator=","
                      :precision="2"
                      :value="hQMaterialSummary.totalImportedMaterialQuantity"
                      title="导入原料总量"
                  ></el-statistic>
                </div>
              </el-col>
              <el-col :span="6">
                <div>
                  <el-statistic title="导入总次数">
                    <template slot="formatter">
                      {{ hQMaterialSummary.totalImportTimes }}
                    </template>
                  </el-statistic>
                </div>
              </el-col>
              <el-col :span="6">
                <div>
                  <el-statistic
                      group-separator=","
                      :precision="2"
                      decimal-separator="."
                      :value="hQMaterialSummary.totalMaterialCategories"
                      title="原料总分类数"
                  >
                    <template slot="prefix">
                      <i class="el-icon-s-flag" style="color: red"></i>
                    </template>
                    <template slot="suffix">
                      <i class="el-icon-s-flag" style="color: blue"></i>
                    </template>
                  </el-statistic>
                </div>
              </el-col>
              <el-col :span="6">
                <div>
                  <el-statistic :value="hQMaterialSummary.totalMaterialTypes" title="原料总类型数">
                    <template slot="suffix">
                <span class="like">
                  <i
                      class="el-icon-star-on"
                      style="color:red"
                  ></i>
                </span>
                    </template>
                  </el-statistic>
                </div>
              </el-col>
            </el-row>
          </div>
        </el-card><br/>
        <el-form :inline="true" v-model="hqListQuery" class="demo-form-inline">
          <el-form-item>
            <el-input
              clearable
              size="small"
              style="width: 180px"
              @change="handleHqList"
              :placeholder="$t('请输入原料分类')"
              v-model="hqListQuery.classification"
            ></el-input>
            <el-input
                clearable
                size="small"
                style="width: 180px"
                @change="handleHqList"
                :placeholder="$t('请输入原料名称')"
                v-model="hqListQuery.name"
            ></el-input>
            <el-button
                type="info"
                size="small"
                @click="handleHqList"
                plain1
                icon="el-icon-search"
            ></el-button>
            <el-button
                size="small"
                @click="getHqList"
                plain1
                icon="el-icon-refresh"
            >{{ $t("刷新列表") }}</el-button>

            <el-button
                size="small"
                type="primary"
                @click="importDialogVisible =true"
                v-shop="false"
                icon="el-icon-upload"
            >{{ $t("导入原料") }}</el-button>
            <el-button
                v-shop="true"
                :disabled="multipleMaterialSelection.length==0"
                size="small"
                type="primary"
                @click="handleApplyMaterialSwitchDialog"
                icon="el-icon-tickets"
            >{{ $t("门店申请原料") }}</el-button>

            <el-button
                size="small"
                @click="getHqAllList"
                plain1
                icon="el-icon-refresh"
            >{{ this.hqListQuery.limit>0? $t("查看全部原料"):$t("关闭查看全部原料") }}</el-button>

          </el-form-item>
        </el-form>

        <el-dialog center
                   width="410px" :title="$t('导入总部原料')" :visible.sync="importDialogVisible">
          <el-upload drag multiple accept=".xlsx,.xls"
                     :headers="headers" :auto-upload="false"
                     :file-list="fileList" ref="importUpload"
                     :action="hqMerchandiseExcelPath">
            <i class="el-icon-upload"></i>
            <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
          </el-upload>
          <p >仅允许导入 xls、xlsx 格式文件。 <el-link type="primary" @click="handleImportTemplate" :underline="false">下载模板</el-link></p>

          <span slot="footer" class="dialog-footer">
            <el-button @click="importDialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="handleImportUpload">确 定</el-button>
          </span>
        </el-dialog>


        <el-dialog
            title="申请"
            :visible.sync="applyMaterialSwitchDialogVisible"
            width="90%">
          <el-card shadow="never">
            <div>
              <el-form :model="applyMaterialForm" size="mini"  ref="ruleForm" label-width="100px" class="demo-ruleForm">
                <el-row>
                  <el-col :span="12">
                    <el-form-item label="申请编号:" prop="name">
                      <el-input disabled v-model="applyMaterialForm.serialNumber"></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="申请时间:" prop="name">
                      {{ formattedDateTime(new Date())}}
                    </el-form-item>
                  </el-col>
                </el-row>
                <el-row>
                  <el-col :span="24">
                    <el-form-item label="备注信息:" prop="region">
                      <el-input type="textarea" :rows="4" placeholder="请输入备注内容"
                                v-model="applyMaterialForm.remark">
                      </el-input>
                    </el-form-item>
                  </el-col>
                </el-row>
              </el-form>
            </div>
            <el-tabs v-model="activeName">
              <el-tab-pane label="用户管理" name="first">
                <el-table  :header-cell-style="{background:'#1988e7',color:'#FFFFFF'}" :data="multipleMaterialSelection" style="width: 100%"  height="250">
                  <template slot="empty">
                    <el-empty description="没有数据"></el-empty>
                  </template>
                  <el-table-column type="index" width="50">
                  </el-table-column>
                  <el-table-column prop="name" label="原料名称" width="180">
                  </el-table-column>
                  <el-table-column
                      align="center"
                      min-width="100"
                      :label="$t('库存量')"
                      prop="hqNumber"
                  >
                    <template slot-scope="scope">
                      <span>{{ scope.row.hqNumber }}</span>
                    </template></el-table-column
                  >

                  <el-table-column
                      align="center"
                      min-width="150"
                      :label="$t('申请量')"
                      prop="hqNumber"
                  >
                    <template slot-scope="scope">
                      <el-input-number size="small"  v-model="scope.row.applyNumber" controls-position="right" :min="0" :max="scope.row.hqNumber"></el-input-number>
                    </template></el-table-column
                  >

                  <el-table-column
                      align="center"
                      prop="property"
                      :label="$t('属性')"
                      width="180"
                  ></el-table-column>

<!--                  <el-table-column
                      align="center"
                      min-width="100"
                      :label="$t('库存量')"
                      prop="hqNumber"
                  >
                    <template slot-scope="scope">
                      <span>{{ scope.row.number }}</span>
                    </template></el-table-column
                  >-->

                  <el-table-column
                      align="center"
                      min-width="100"
                      :label="$t('规格')"
                      prop="materialType"
                  >
                    <template slot-scope="scope">
                      <span>{{ scope.row.standard }}</span>
                    </template></el-table-column
                  >

                  <el-table-column
                      align="center"
                      min-width="100"
                      :label="$t('原料分类')"
                      prop="materialType"
                  >
                    <template slot-scope="scope">
                      <span>{{ scope.row.classification }}</span>
                    </template></el-table-column
                  >


                  <el-table-column
                      align="center"
                      min-width="100"
                      :label="$t('基本单位')"
                      prop="materialType"
                  >
                    <template slot-scope="scope">
                      <span>{{ scope.row.basicUnit }}</span>
                    </template></el-table-column
                  >

                  <el-table-column
                      align="center"
                      min-width="100"
                      :label="$t('标签')"
                      prop="materialType"
                  >
                    <template slot-scope="scope">
                      <span>{{ scope.row.label }}</span>
                    </template></el-table-column
                  >


                  <el-table-column
                      align="center"
                      min-width="100"
                      :label="$t('品牌')"
                      prop="materialType"
                  >
                    <template slot-scope="scope">
                      <span>{{ scope.row.brand }}</span>
                    </template></el-table-column
                  >

                  <el-table-column
                      align="center"
                      min-width="100"
                      :label="$t('原料单位')"
                      prop="materialType"
                  >
                    <template slot-scope="scope">
                      <span>{{ scope.row.unit }}</span>
                    </template></el-table-column
                  >
                  <el-table-column
                      align="center"
                      min-width="100"
                      :label="$t('盘点单位')"
                      prop="materialType"
                  >
                    <template slot-scope="scope">
                      <span>{{ scope.row.checkUnit }}</span>
                    </template></el-table-column
                  >

                  <el-table-column
                      align="center"
                      :label="$t('Operate')"
                      width="150"
                      class-name="small-padding fixed-width"
                  >
                    <template slot="header" slot-scope="{ row }">
                      {{ $t("Operate") }}
                      <i class="el-icon-setting" style="color: #d35e32"></i>
                    </template>

                    <template slot-scope="scope">
                      <el-row>
                        <el-link type="danger" :underline="false">
                          <i class="el-icon-circle-close" @click="handelDeleteMultipleMaterialSelection(scope.row)" ></i>
                        </el-link>
                      </el-row>
                    </template>
                  </el-table-column>
                </el-table>
              </el-tab-pane>
            </el-tabs>
          </el-card>
          <span slot="footer" class="dialog-footer">
            <el-button @click="applyMaterialSwitchDialogVisible = false">取 消</el-button>
            <el-button type="primary" :disabled="multipleMaterialSelection.length==0" @click="handelSubmitMultipleMaterialSelection">确 定</el-button>
          </span>
        </el-dialog>

        <el-table
            :header-cell-style="{background: this.hqListQuery.limit>0?'#eef1f6':'#5a718f',color:this.hqListQuery.limit>0?'#606266':'#fdfdfd'}"
            v-loading="hqListLoading"
            :element-loading-text="$t('拼命加载中')"
            element-loading-spinner="el-icon-loading"
            element-loading-background="rgba(0, 0, 0, 0.5)"
            ref="handMaterialSelectMultipleTable"
            :data="hqList"
            :row-key="row => row.id"
            :height="this.hqListQuery.limit>0? 650:1000"
            stripe
            @selection-change="handleHqMerchandiseSelectionChange"
            @select="handleSelectRow"
            @select-all="handleSelectAll"
        >
          <template slot="empty">
            <el-empty description="没有数据"></el-empty>
          </template>
          <el-table-column   type="selection"
              width="55">
          </el-table-column>
          <el-table-column
              type="index"
              :label="$t('序号')"
              :index="(index) => getIndex(index, hqListQuery.page, hqListQuery.limit)"
              width="50">
          </el-table-column>

          <el-table-column
            align="center"
            min-width="100"
            :label="$t('原料分类')"
            prop="materialType"
          >
            <template slot-scope="scope">
              <span>{{ scope.row.classification }}</span>
            </template></el-table-column
          >
          <el-table-column
            align="center"
            min-width="100"
            :label="$t('原料品牌')"
            prop="materialType"
          >
            <template slot-scope="scope">
              <span>{{ scope.row.brand }}</span>
            </template></el-table-column
          >
          <el-table-column
            align="center"
            prop="name"
            :label="$t('原料名称')"
            width="180"
          ></el-table-column>

          <el-table-column
              align="center"
              min-width="100"
              :label="$t('库存量')"
              prop="hqNumber"
          >
            <template slot-scope="scope">
              <span>{{ scope.row.hqNumber }}</span>
            </template></el-table-column
          >

          <el-table-column
            align="center"
            min-width="100"
            :label="$t('进货单位')"
            prop="materialType"
          >
            <template slot-scope="scope">
              <span>{{ scope.row.unit }}</span>
            </template></el-table-column
          >

          <el-table-column
              align="center"
              prop="property"
              :label="$t('属性')"
              width="180"
          ></el-table-column>

<!--          <el-table-column
              align="center"
              min-width="100"
              :label="$t('库存量')"
              prop="hqNumber"
          >
            <template slot-scope="scope">
              <span>{{ scope.row.number }}</span>
            </template></el-table-column
          >-->

          <el-table-column
              align="center"
              min-width="100"
              :label="$t('原料规格')"
              prop="materialType"
          >
            <template slot-scope="scope">
              <span>{{ scope.row.standard }}</span>
            </template></el-table-column
          >

          <el-table-column
              align="center"
              min-width="100"
              :label="$t('基本单位')"
              prop="materialType"
          >
            <template slot-scope="scope">
              <span>{{ scope.row.basicUnit }}</span>
            </template></el-table-column
          >

          <el-table-column
              align="center"
              min-width="100"
              :label="$t('标签')"
              prop="materialType"
          >
            <template slot-scope="scope">
              <span>{{ scope.row.label }}</span>
            </template></el-table-column
          >

          <el-table-column
              align="center"
              min-width="100"
              :label="$t('盘点单位')"
              prop="materialType"
          >
            <template slot-scope="scope">
              <span>{{ scope.row.checkUnit }}</span>
            </template></el-table-column
          >
        </el-table>

        <pagination
            :autoScroll="false"
            v-show="hqListQuery.total > 0"
            :total="hqListQuery.total"
            :page.sync="hqListQuery.page"
            :limit.sync="hqListQuery.limit"
            @pagination="getHqList"
        />
      </el-card>
      <br/>

<!--      导入总部原料记录-->
      <el-card class="box-card">
        <el-divider content-position="left"><i class="el-icon-s-order"></i>{{$t('导入总部原料记录列表')}}</el-divider>
        <el-form :inline="true" v-model="hqImportLogListQuery" class="demo-form-inline">
          <el-form-item>
            <el-input
                clearable
                size="small"
                style="width: 180px"
                :placeholder="$t('请输入文件名称')"
                v-model="hqImportLogListQuery.name"
            ></el-input>
            <el-button
                type="info"
                size="small"
                @click="getHqImportLogList"
                plain1
                icon="el-icon-search"
            ></el-button>
            <el-button
                size="small"
                @click="getHqImportLogList"
                plain1
                icon="el-icon-refresh"
            >{{ $t("刷新列表") }}</el-button>
          </el-form-item>
        </el-form>

        <el-table
            v-loading="hqImportLogListLoading"
            :element-loading-text="$t('拼命加载中')"
            element-loading-spinner="el-icon-loading"
            element-loading-background="rgba(0, 0, 0, 0.5)"
            ref="handSelectTest_multipleTable"
            :data="hqImportLogList"
            stripe
            border
            height="450"
            :header-cell-style="{background:'#eef1f6',color:'#606266'}"
        >
          <template slot="empty">
            <el-empty description="没有数据"></el-empty>
          </template>
          <el-table-column
              type="index"
              :index="(index) => getIndex(index, hqImportLogListQuery.page, hqImportLogListQuery.limit)"
              :label="$t('序号')"
              width="50">
          </el-table-column>
          <el-table-column
              align="center"
              prop="name"
              :label="$t('原料资料文件名称')"
          ></el-table-column>

          <el-table-column
              align="center"
              min-width="100"
              :label="$t('导入时间')"
              prop="materialType"
          >
            <template slot-scope="scope">
              <span>{{ formattedDateTime(scope.row.addTime) }}</span>
            </template></el-table-column
          >

          <el-table-column
              align="center"
              :label="$t('Operate')"
              width="150"
              class-name="small-padding fixed-width"
          >
            <template slot="header" slot-scope="{ row }">
              {{ $t("Operate") }}
              <i class="el-icon-setting" ></i>
            </template>

            <template slot-scope="scope">
              <el-row>
                <el-col :span="24">
                  <el-button
                      type="text"
                      icon="el-icon-view"
                      size="small"
                      @click="handleHqImportList(scope.row)"
                  >{{ $t("查看") }}
                  </el-button>
                </el-col>
              </el-row>
            </template>
          </el-table-column>
        </el-table>

        <pagination
            :autoScroll="false"
            v-show="hqImportLogListQuery.total > 0"
            :total="hqImportLogListQuery.total"
            :page.sync="hqImportLogListQuery.page"
            :limit.sync="hqImportLogListQuery.limit"
            @pagination="getHqImportLogList"
        />
      </el-card>

      <el-dialog
          title="导入原料详情列表"
          :visible.sync="hqImportListDialogVisible"
          width="90%">
        <el-table
            :header-cell-style="{background:'#1988e7',color:'#FFFFFF'}"
            v-loading="hqImportListLoading"
            :element-loading-text="$t('拼命加载中')"
            element-loading-spinner="el-icon-loading"
            element-loading-background="rgba(0, 0, 0, 0.5)"
            ref="handSelectTest_multipleTable"
            :data="hqImportList"
            stripe
            border
            height="450"
        >
          <template slot="empty">
            <el-empty description="没有数据"></el-empty>
          </template>
          <el-table-column
              type="index"
              :label="$t('序号')"
              width="50">
          </el-table-column>
          <el-table-column
              align="center"
              prop="name"
              :label="$t('原料名称')"
              width="180"
          ></el-table-column>

          <el-table-column
              align="center"
              prop="property"
              :label="$t('属性')"
              width="180"
          ></el-table-column>

          <el-table-column
              align="center"
              min-width="100"
              :label="$t('数量')"
              prop="hqNumber"
          >
            <template slot-scope="scope">
              <span>{{ scope.row.number }}</span>
            </template></el-table-column
          >

          <el-table-column
              align="center"
              min-width="100"
              :label="$t('规格')"
              prop="materialType"
          >
            <template slot-scope="scope">
              <span>{{ scope.row.standard }}</span>
            </template></el-table-column
          >

          <el-table-column
            align="center"
            min-width="100"
            :label="$t('原料分类')"
            prop="materialType"
          >
            <template slot-scope="scope">
              <span>{{ scope.row.classification }}</span>
            </template></el-table-column
          >


          <el-table-column
              align="center"
              min-width="100"
              :label="$t('基本单位')"
              prop="materialType"
          >
            <template slot-scope="scope">
              <span>{{ scope.row.basicUnit }}</span>
            </template></el-table-column
          >

          <el-table-column
            align="center"
            min-width="100"
            :label="$t('标签')"
            prop="materialType"
          >
            <template slot-scope="scope">
              <span>{{ scope.row.label }}</span>
            </template></el-table-column
          >


          <el-table-column
            align="center"
            min-width="100"
            :label="$t('品牌')"
            prop="materialType"
          >
            <template slot-scope="scope">
              <span>{{ scope.row.brand }}</span>
            </template></el-table-column
          >

          <el-table-column
              align="center"
              min-width="100"
              :label="$t('原料单位')"
              prop="materialType"
          >
            <template slot-scope="scope">
              <span>{{ scope.row.unit }}</span>
            </template></el-table-column
          >
          <el-table-column
            align="center"
            min-width="100"
            :label="$t('盘点单位')"
            prop="materialType"
          >
            <template slot-scope="scope">
              <span>{{ scope.row.checkUnit }}</span>
            </template></el-table-column
          >


        </el-table>

        <pagination
            :autoScroll="false"
            v-show="hqImportListQuery.total > 0"
            :total="hqImportListQuery.total"
            :page.sync="hqImportListQuery.page"
            :limit.sync="hqImportListQuery.limit"
            @pagination="getHqImportList"
        />
        <span slot="footer" class="dialog-footer">
          <el-button @click="hqImportListDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="hqImportListDialogVisible = false">确 定</el-button>
        </span>
      </el-dialog>

    </div>
  </div>
</template>
<script>

import {nanoid} from "nanoid";
import Pagination from "@/components/Pagination"; // Secondary package based on el-pagination
import { hqList,hqImportLogList,hqImportList,shopReview,hQMaterialSummary} from "@/api/masterInventory";
import { getToken } from "@/utils/auth";
import {hqMerchandiseExcelPath} from '@/api/storage'
import {formatDate, formattedDateTime} from "../../utils/date";
export default {
  name: "masterInventory",
  components: { Pagination },
  data() {
    return {
      //统计
      hQMaterialSummary:{
        totalImportedMaterialQuantity:0,
        totalImportTimes:0,
        totalMaterialCategories:0,
        totalMaterialTypes:0,
      },

      //总部原料列表加载
      hqListLoading:false,
      //总部原料列表
      hqList:[],
      hqListQuery: {
        name: undefined,
        classification: undefined,
        page: 1,
        limit: 10,
        total: 1,
      },

      //导入总部原料开关
      importDialogVisible:false,
      fileList:[],
      //申请原料选中的数据行
      multipleMaterialSelection: [],
      //申请总部原料开关
      applyMaterialSwitchDialogVisible:false,
      activeName:'first',

      //申请表单
      applyMaterialForm:{
        serialNumber:undefined,
        remark:'',
        multipleMaterialSelectionList:[],
      },
      //导入总部原料接口
      hqMerchandiseExcelPath,


      //总部原料导入记录列表加载
      hqImportLogListLoading:false,
      //总部原料导入记录列表
      hqImportLogList:[],
      hqImportLogListQuery: {
        name: undefined,
        page: 1,
        limit: 10,
        total: 1,
      },

      //总部原料导入记录列表开关
      hqImportListDialogVisible:false,
      //总部原料导入记录列表加载
      hqImportListLoading:false,
      //总部原料导入记录列表
      hqImportList:[],
      hqImportListQuery: {
        logId:undefined,
        page: 1,
        limit: 10,
        total: 1,
      },

    };
  },
  computed: {
    headers() {
      return {
        'X-Litemall-Admin-Token': getToken()
      }
    }
  },
  created() {
    //加载总部门店原料
    this.getHqList();

    //加载总部原料导入记录
    this.getHqImportLogList();

    //总部原料-描述列表总值
    this.getHQMaterialSummary()
  },
  methods: {
    formattedDateTime,formatDate,
    //总部原料-描述列表总值
    getHQMaterialSummary(){
      hQMaterialSummary().then((response) => {
        this.hQMaterialSummary = response.data.data
      })
    },
    getHqAllList() {
      this.hqListQuery.limit = this.hqListQuery.limit>0? -1:10;
      this.getHqList();
    },
    handleHqList(){
      this.hqListQuery.page = 1;
      this.getHqList()
    },
    //获取总部门店原料
    getHqList() {
      this.hqListLoading = true;
      hqList(this.hqListQuery).then((response) => {
        this.hqList = response.data.data.list;
        this.hqListQuery.total = response.data.data.total;
        this.hqListLoading = false;

        // 勾选当前页中在 multipleMaterialSelection 中已存在的项
        this.$nextTick(() => {
          this.hqList.forEach(row => {
            if (this.multipleMaterialSelection.some(item => item.id === row.id)) {
              this.$refs.handMaterialSelectMultipleTable.toggleRowSelection(row, true);
            }
          });
        });
      })
        .catch(() => {
          this.hqList = [];
          this.hqListQuery.total = 0;
          this.hqListLoading = false;
        });
    },
    //序号叠加
    getIndex(index, page, limit) {
      return (index + 1) + (page - 1) * limit;
    },
    // 选择总部原料申请调货：追加而不是替换
    handleHqMerchandiseSelectionChange(val) {
      val.forEach(item => {
        if (!this.multipleMaterialSelection.includes(item)) {
          this.multipleMaterialSelection.push(item);
        }
      });
    },
    // 单行选择或取消勾选
    handleSelectRow(selection, row) {
      const exists = this.multipleMaterialSelection.find(item => item.id === row.id);

      if (selection.find(item => item.id === row.id)) {
        // 勾选了这一项，追加（不重复）
        if (!exists) {
          this.multipleMaterialSelection.push(row);
        }
      } else {
        // 取消了这一项，移除
        this.multipleMaterialSelection = this.multipleMaterialSelection.filter(
          item => item.id !== row.id
        );
      }
    },

// 全选或取消全选
    handleSelectAll(selection) {
      const currentPageIds = this.hqList.map(item => item.id);

      // 添加当前页中被勾选的项
      selection.forEach(item => {
        if (!this.multipleMaterialSelection.find(sel => sel.id === item.id)) {
          this.multipleMaterialSelection.push(item);
        }
      });

      // 移除当前页中未被勾选的项
      this.multipleMaterialSelection = this.multipleMaterialSelection.filter(item => {
        return !currentPageIds.includes(item.id) || selection.find(sel => sel.id === item.id);
      });
    },
    //点击原料申请
    handleApplyMaterialSwitchDialog(){
      this.applyMaterialSwitchDialogVisible = true;
      // 获取当前时间戳
      const timestamp = Date.now();
      // 将时间戳赋值给申请材料表单的序列号字段
      this.applyMaterialForm.serialNumber = 'SQ' + nanoid();
    },
    // 删除选择总部原料申请行内容
    handelDeleteMultipleMaterialSelection(row) {
      // 根据 row 的 id 过滤掉要删除的行
      this.multipleMaterialSelection = this.multipleMaterialSelection.filter(item => item.id!== row.id);
    },
    // 提交门店原料申请
    handelSubmitMultipleMaterialSelection() {
      const emptyApplyNumberMaterials = [];
      const zeroApplyNumberMaterials = [];

      this.multipleMaterialSelection.forEach(item => {
        const num = Number(item.applyNumber);
        if (item.applyNumber === null || item.applyNumber === undefined || item.applyNumber === '') {
          emptyApplyNumberMaterials.push(item.name);
        } else if (num <= 0) {
          zeroApplyNumberMaterials.push(item.name);
        }
      });

      if (emptyApplyNumberMaterials.length > 0) {
        const message = `以下原料的申请量未填写：${emptyApplyNumberMaterials.join('、')}`;
        this.$message.warning(message);
        return;
      }

      if (zeroApplyNumberMaterials.length > 0) {
        const message = `以下原料的申请量必须大于 0：${zeroApplyNumberMaterials.join('、')}`;
        this.$message.warning(message);
        return;
      }

      // 通过校验，准备提交
      this.applyMaterialForm.multipleMaterialSelectionList = this.multipleMaterialSelection;
      shopReview(this.applyMaterialForm)
        .then(() => {
          this.multipleMaterialSelection = [];
          this.applyMaterialForm.serialNumber = undefined;
          this.applyMaterialForm.remark = '';
          this.applyMaterialForm.multipleMaterialSelectionList = [];
          this.applyMaterialSwitchDialogVisible = false;
          this.$refs.handMaterialSelectMultipleTable.clearSelection();
          this.$notify.success({
            title: this.$t('Success!'),
            message: '提交申请成功！'
          });
        })
        .catch(() => {
          this.$notify.error({
            title: this.$t('Error!'),
            message: '提交申请失败！'
          });
        });
    },
    // 提交导入原料
    handleImportUpload() {
      this.$refs.importUpload.submit();
      this.$notify.success({
        title: this.$t('Success!'),
        message: '资料已提交上传'
      });
      this.importDialogVisible = false;
      this.fileList = [];

      // 使用 setTimeout 延迟 3 秒（3000 毫秒）后执行 getHqImportLogList 方法
      setTimeout(() => {
        this.getHqImportLogList();
        this.getHQMaterialSummary();
      }, 5000);
    },

    //下载导入总部原料excel模版
    handleImportTemplate() {
      const url = "https://app.lumierecafe.com/mi/lumierecafe/app/%E5%8E%9F%E6%96%99%E5%AF%BC%E5%85%A5%E6%A8%A1%E6%9D%BF.xlsx";
      const link = document.createElement("a");
      link.href = url;
      link.download = "原料导入模板.xlsx"; // 文件名
      document.body.appendChild(link);
      link.click();
      document.body.removeChild(link);
    },

    //获取导入总部原料记录
    getHqImportLogList(){
      this.hqImportLogListLoading = true;
      hqImportLogList(this.hqImportLogListQuery).then((response) => {
        this.hqImportLogList = response.data.data.list;
        this.hqImportLogListQuery.total = response.data.data.total;
        this.hqImportLogListLoading = false;
      })
          .catch(() => {
            this.hqImportLogList = [];
            this.hqImportLogListQuery.total = 0;
            this.hqImportLogListLoading = false;
          });
    },
    //加载导入总部原料记录列表详情
    handleHqImportList(row){
      this.hqImportListDialogVisible = true;
      this.hqImportListQuery.logId = row.id
      this.getHqImportList();
    },

    getHqImportList(){
      this.hqImportListDialogVisible = true;
      hqImportList(this.hqImportListQuery).then((response) => {
        this.hqImportList = response.data.data.list;
        this.hqImportListQuery.total = response.data.data.total;
        this.hqImportListLoading = false;
      })
          .catch(() => {
            this.hqImportList = [];
            this.hqImportListQuery.total = 0;
            this.hqImportListLoading = false;
          });
    },


  }
};
</script>
<style scoped>
</style>
